package abg.core.mapper;

import abg.core.domain.db.MtRoomUserAuth;
import com.mybatisflex.core.query.QueryWrapper;

import java.util.List;

import static abg.core.domain.db.table.MtRoomUserAuthTableDef.MT_ROOM_USER_AUTH;

public interface MtRoomUserAuthMapper extends ZBMapper<MtRoomUserAuth> {


    default MtRoomUserAuth getByMtIdAndUserId(String mtId, String userId) {
        return selectOneByQuery(
                QueryWrapper.create()
                        .and(MT_ROOM_USER_AUTH.MT_ID.eq(mtId))
                        .and(MT_ROOM_USER_AUTH.USER_ID.eq(userId))
        );
    }

    default int updateByMtIdUserId(MtRoomUserAuth update, String mtId, String userId) {
        return updateByQuery(update, QueryWrapper.create()
                .and(MT_ROOM_USER_AUTH.MT_ID.eq(mtId))
                .and(MT_ROOM_USER_AUTH.USER_ID.eq(userId))
        );
    }

    default boolean getIsDisableInMt(String userId, String mtId) {
        String isOut = selectOneByQueryAs(
                QueryWrapper.create().select("is_out")
                        .and(MT_ROOM_USER_AUTH.MT_ID.eq(mtId))
                        .and(MT_ROOM_USER_AUTH.USER_ID.eq(userId)),
                String.class
        );

        return "1".equals(isOut);
    }

    default long getRoomNowUserCount(String mtId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .and(MT_ROOM_USER_AUTH.MT_ID.eq(mtId))
                .and(MT_ROOM_USER_AUTH.IS_IN_ROOM.eq("1"));
        return selectCountByQuery(wrapper);
    }

    default List<String> getNowUserIds(String mtId) {
        QueryWrapper wrapper = QueryWrapper.create().select("user_id").eq("mt_id", mtId);
        return selectObjectListByQueryAs(wrapper, String.class);
    }
}